﻿using BLL;
using IBLL;
using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.登录老师账号界面;

namespace WindowsFormsApp1
{

    public partial class FrExamManage : Form
    {
        List<StudentClass_MD> ClassMD = null;
        IFrExamManage_IBLL frExam = new FrExamManage_BLL();
        IFrStudentManage_IBLL frStudent = new FrStudentManage_BLL();
        IFrQuestionBankManage_IBLL frQuestion = new FrQuestionBankManage_BLL();
        public FrExamManage()
        {
            InitializeComponent();
            ClassMD= frStudent.SClass();
            ClassMD.Insert(0, new StudentClass_MD()
            {
                ClassId = 0,
                ClassName="全部查询"
            });
            ClassSelect.DataSource = ClassMD;
            ClassSelect.DisplayMember = "ClassName";
            ClassSelect.ValueMember = "ClassId";
            ClassSelect_SelectedIndexChanged(null, null);
        }
        
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EcamCorrect correct = new EcamCorrect();
            correct.Show();
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
          
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FrAddExam frAdd = new FrAddExam();
            DialogResult dialog= frAdd.ShowDialog();
            if (dialog==DialogResult.OK)
            {
                Query_TextChanged(null, null);
            }
        }

        private void ClassSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.TryParse(ClassSelect.SelectedValue.ToString(),out int s) &&(int)ClassSelect.SelectedValue == 0)
            {
              uiDataGridView1.DataSource=  frExam.ExamInfo_s();
            }
            else
            {
                uiDataGridView1.DataSource= frExam.ExamInfo_s($" where ClassName='{ClassSelect.Text}';");
            }
        }

        private void Query_TextChanged(object sender, EventArgs e)
        {
            string rea = "";
            if ((int)ClassSelect.SelectedValue != 0)
            {
                rea = $" ClassName={ClassSelect.Text} and";
            }
            string reader = null;
            if (int.TryParse(Query.Text,out int kk))
            {
                reader = $"   where {rea} PapersNumber= {Query.Text} ";
            }
            else
            {
                reader = $"  where {rea} PapersName like '%{Query.Text}%' ";
            }

            uiDataGridView1.DataSource = frExam.ExamInfo_s(reader);

        }

        private void uiDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (uiDataGridView1.SelectedRows.Count < 0 || e.RowIndex < 0)
            {
                return;
            }

            uiDataGridView1.Rows[e.RowIndex].Selected = true;
            if (uiDataGridView1.SelectedRows.Count!=1)
            {
                return;
            }
            EcamCorrect correct = new EcamCorrect(uiDataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            correct.ShowDialog();
        }

        private void uiDataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (uiDataGridView1.SelectedRows.Count < 0 || e.RowIndex < 0)
            {
                return;
            }

            uiDataGridView1.Rows[e.RowIndex].Selected = true;
        }

        private void FrExamManage_Load(object sender, EventArgs e)
        {
            uiDataGridView1.ClearSelection();
        }

        private void uiDataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            uiDataGridView1.ClearSelection();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string s = $"update Exam_info set status=1 where PapersNumber=' ";
            string s1 = "";
            foreach (DataGridViewRow item in uiDataGridView1.SelectedRows)
            {
                s1 += s + item.Cells[0].Value + " ';";
            }
            frQuestion.SubMit(s1);
            Query_TextChanged(null, null);
        }
    }
}
